import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Layout from '@/views/layout'
import Home from '@/views/home'
import Video from '@/views/video'
import Question from '@/views/question'
import User from '@/views/user'
import Search from '@/views/search'
import SearchResult from '@/views/search/result'
import Article from '@/views/article'
import Profile from '@/views/user/profile'
import Chat from '@/views/user/chat'
// import store from '@src/store'

// 为了解决在路由导航守卫中，发生路由跳转时的错误异常
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    name: 'layout',
    component: Layout,
    children: [
      // 子路由-二级路由
      {
        path: '/',
        name: 'home',
        component: Home,
        meta: { isKeepAlive: true }
      },
      {
        path: '/video',
        name: 'video',
        component: Video
      },
      {
        path: '/question',
        name: 'question',
        component: Question
      },
      {
        path: '/user',
        name: 'user',
        component: User
      }
    ]
  },
  {
    path: '/search',
    name: 'search',
    component: Search
  },
  {
    path: '/search/result',
    name: 'searchResult',
    component: SearchResult
  },
  {
    path: '/article/:id',
    name: 'article',
    component: Article
  },
  {
    path: '/user/profile',
    name: 'profile',
    component: Profile
  },
  {
    path: '/user/chat',
    name: 'chat',
    component: Chat
  }
]
const router = new VueRouter({
  routes
})
export default router
